import sys
from PyQt5.QtWidgets import QSlider, QApplication, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QGroupBox
from PyQt5.QtCore import Qt

class ImageProcessingControls(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.setup_processing_controls()
    
    def setup_ui(self):
        layout = QVBoxLayout()
        
        # 亮度控制
        self.brightness_slider = self.create_control_slider("亮度", -100, 100, 0)
        
        # 对比度控制
        self.contrast_slider = self.create_control_slider("对比度", -100, 100, 0)
        
        # 饱和度控制
        self.saturation_slider = self.create_control_slider("饱和度", -100, 100, 0)
        
        # 色相控制（循环值）
        self.hue_slider = self.create_control_slider("色相", 0, 359, 0)
        #self.hue_slider.setTickPosition(QSlider.TicksBelow)
        #self.hue_slider.setTickInterval(30)
        
        layout.addWidget(self.brightness_slider['group'])
        layout.addWidget(self.contrast_slider['group'])
        layout.addWidget(self.saturation_slider['group'])
        layout.addWidget(self.hue_slider['group'])
        self.setLayout(layout)
    
    def create_control_slider(self, label, min_val, max_val, default_val):
        """创建带标签的滑块控件"""
        group = QGroupBox(label)
        layout = QVBoxLayout()
        
        slider = QSlider(Qt.Horizontal)
        slider.setRange(min_val, max_val)
        slider.setValue(default_val)
        
        value_label = QLabel(str(default_val))
        
        # 连接信号更新标签
        slider.valueChanged.connect(
            lambda v: value_label.setText(str(v))
        )
        
        layout.addWidget(slider)
        layout.addWidget(value_label)
        group.setLayout(layout)
        
        return {
            'group': group,
            'slider': slider,
            'label': value_label
        }
    
    def setup_processing_controls(self):
        # 连接所有滑块的实时更新
        controls = [self.brightness_slider, self.contrast_slider, 
                   self.saturation_slider, self.hue_slider]
        
        for control in controls:
            control['slider'].valueChanged.connect(self.on_parameter_changed)
    
    def on_parameter_changed(self):
        """任何参数改变时的处理"""
        brightness = self.brightness_slider['slider'].value()
        contrast = self.contrast_slider['slider'].value()
        saturation = self.saturation_slider['slider'].value()
        hue = self.hue_slider['slider'].value()
        
        print(f"参数更新 - 亮度: {brightness}, 对比度: {contrast}, "
              f"饱和度: {saturation}, 色相: {hue}")
        
        # 应用图像处理效果
        self.apply_image_effects(brightness, contrast, saturation, hue)
    
    def apply_image_effects(self, brightness, contrast, saturation, hue):
        """应用图像处理效果"""
        # 实际的图像处理逻辑
        pass
    
    def get_parameters(self):
        """获取所有参数值"""
        return {
            'brightness': self.brightness_slider['slider'].value(),
            'contrast': self.contrast_slider['slider'].value(),
            'saturation': self.saturation_slider['slider'].value(),
            'hue': self.hue_slider['slider'].value()
        }



if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ImageProcessingControls()
    window.show()
    sys.exit(app.exec_())